package cn.xlbweb.sso.autoconfigure;

import cn.xlbweb.sso.constant.SsoProperties;
import cn.xlbweb.sso.config.SsoWebConfig;
import cn.xlbweb.sso.filter.SsoFilter;
import cn.xlbweb.sso.listener.SsoSessionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.web.client.RestTemplate;

/**
 * @author: bobi
 * @date: 2019-03-09 16:57
 * @description:
 */
@Configuration
@EnableConfigurationProperties(SsoProperties.class)
@Import({SsoFilter.class, SsoSessionListener.class, SsoWebConfig.class})
public class SsoConfiguration {

    @Autowired
    private SsoFilter loginFilter;

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        System.out.println("filterRegistrationBean...");
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(loginFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return filterRegistrationBean;
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
